(ns
  com.marzhillstudios.molehill.test-file
  (:import [org.joda.time.format DateTimeFormat])
  (:require [clojure.contrib.str-utils2 :as s])
  (:use [clojure.test]
     [com.marzhillstudios.molehill.file]))


(def test-date "1970-01-01 00:00:00")
(def test-epoch (.getMillis (.parseDateTime
                             (DateTimeFormat/forPattern
                               "yyyy-MM-dd HH:mm:ss")
                             test-date)))

(deftest test-file-struct-construction
         (let [fstruct (new-hill-file "foo")]
           (is (= (:title fstruct) "foo")))
         (let [fstruct (new-hill-file "foo" "foo content")]
           (is (= (:title fstruct) "foo"))
           (is (= (:content fstruct "foo content"))))
         (let [fstruct (new-hill-file "foo" "foo content"
                                      "1970-01-01 00:00:00"
                                      ["tag1" "tag2"]
                                      "foo author")]
           (is (= (:title fstruct) "foo"))
           (is (= (:content fstruct "foo content")))
           (is (= (:date fstruct) "1970-01-01 00:00:00"))
           (is (= (:epoch fstruct) test-epoch))
           (is (= (:tags fstruct) ["tag1" "tag2"]))
           (is (= (:author fstruct) "foo author")))
         (let [fstruct (new-hill-file "foo" "foo content"
                                      (mk-date-struct
                                        "1970-01-01 00:00:00"
                                        "yyyy-MM-dd HH:mm:ss")
                                      ["tag1" "tag2"]
                                      "foo author")]
           (is (= (:epoch fstruct) test-epoch))))

(deftest test-hill-file-meta
         (let [fstruct (new-hill-file "foo" "foo content"
                                      (mk-date-struct
                                        "1970-01-01 00:00:00"
                                        "yyyy-MM-dd HH:mm:ss")
                                      ["tag1" "tag2"]
                                      "foo author")]
           (is (= (:title (hill-file-meta fstruct)) (:title fstruct)))
           (is (= (:date (hill-file-meta fstruct)) (:date fstruct)))
           (is (= (:epoch (hill-file-meta fstruct)) (:epoch fstruct)))
           (is (= (:tags (hill-file-meta fstruct)) (:tags fstruct)))
           (is (= (:author (hill-file-meta fstruct)) (:author fstruct)))))

(deftest test-hill-file-slug
         (let [fstruct (new-hill-file "foo bar none" "foo content"
                                      (mk-date-struct
                                        "1970-01-01 00:00:00"
                                        "yyyy-MM-dd HH:mm:ss")
                                      ["tag1" "tag2"]
                                      "foo author")]
           (is (= (hill-file-slug fstruct) "foo-bar-none")))
         (let [fstruct (new-hill-file
                         (s/join "" (take 32 (repeatedly (fn [] \z))))
                         "foo content"
                         (mk-date-struct
                           "1970-01-01 00:00:00"
                           "yyyy-MM-dd HH:mm:ss")
                         ["tag1" "tag2"]
                         "foo author")]
           (is (= (hill-file-slug fstruct) "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"))))

(deftest test-sanitize-file-name
         (is (= (sanitize-text "foo \"bar\" and 'mr. bleh';")
                "foo-bar-and-mr-bleh")))
; TODO(jwall): add tests for to and from file serialization
